//
//  BaiduMapManager.m
//  BaiduMapDemo
//
//  Created by 赵博 on 2019/6/3.
//  Copyright © 2019 赵博. All rights reserved.
//

#import "BaiduMapManager.h"
#import <BMKLocationKit/BMKLocationComponent.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
@interface BaiduMapManager()<BMKLocationAuthDelegate,BMKLocationManagerDelegate>
@property(nonatomic, strong)BMKLocationManager *locationManager;
@end
@implementation BaiduMapManager
+ (instancetype)shareManager{
    static BaiduMapManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[BaiduMapManager alloc] init];
    });
    return manager;
}

- (BOOL)validationAppKey:(NSString *)appKey{
    BMKMapManager *mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [mapManager start:appKey  generalDelegate:nil];
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:appKey authDelegate:self];
    return ret;
}

- (void)configLocationManagerAndAllowsBackgroundLocation:(BOOL)allowsBackgroundLocation{
    //初始化实例
    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    _locationManager.allowsBackgroundLocationUpdates = allowsBackgroundLocation;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 10;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;
}
#pragma mark - 单次定位
- (void)singleLocation:(void (^)(BMKLocation *response))completionBlock failureBlock:(void (^)( NSError * err))failureBlock{
    [_locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        if (error) {
            failureBlock(error);
        }else{
            completionBlock(location);
        }
    }];
}
#pragma mark - 开启持续定位
- (void)openContinualPositioning{
    [_locationManager startUpdatingLocation];
    ///如果需要持续定位返回地址信息（需要联网），请设置如下：
//    [self.locationManager setLocatingWithReGeocode:YES];
//    [self.locationManager startUpdatingLocation];
}
#pragma mark - 关闭持续定位
- (void)closeContinualPositioning{
    [self.locationManager stopUpdatingLocation];
}

#pragma mark - 持续定位delegate
- (void)BMKLocationManager:(BMKLocationManager *)manager didUpdateLocation:(BMKLocation *)location orError:(NSError *)error{
    
    !self.continualblock ?: self.continualblock(location,error);

}
- (void)version{
    NSLog(@"版本号BMKLocationKit~>1.5.0 && BaiduMapKit~>4.3.0");
}
@end
